#include <control.h>
#include <game.h>
#include <stdlib.h>

void initGame(FILE *map, field **fld){

  char buf;
  int i, j;
  int magn;

  i=0;
  j=0;
  while(fread(&buf, 1, 1, map) > 0){
    if(buf != 0x00){
      fld[i][j].clr = (color)buf;
      fread(&buf, 1, 1, map);
      fld[i][j].isCapital = (bool)buf;
      fread(&buf, 1, 1, map);
      fld[i][j].isTree = (bool)buf;

      linkHexagon(fld, &(fld[i][j]), i-1, j, 0);
      linkHexagon(fld, &(fld[i][j]), i+1, j, 3);
      if(j%2 == 0){
        linkHexagon(fld, &(fld[i][j]), i-1, j+1, 1);
        linkHexagon(fld, &(fld[i][j]), i, j+1, 2);
        linkHexagon(fld, &(fld[i][j]), i, j-1, 4);
        linkHexagon(fld, &(fld[i][j]), i-1, j-1, 5);
      }
      else{
        linkHexagon(fld, &(fld[i][j]), i, j+1, 1);
        linkHexagon(fld, &(fld[i][j]), i+1, j+1, 2);
        linkHexagon(fld, &(fld[i][j]), i+1, j-1, 4);
        linkHexagon(fld, &(fld[i][j]), i, j-1, 5);
      }
    }

    j++;
    if(j == 30){
      i++;
      j=0;
    }
    if(i == 30)
      break;

  }

  for(i=0; i<30; i++){
    for(j=0; j<30; j++){
      if(fld[i][j].isCapital == TRUE){
        if(buildTerritory(&(fld[i][j])) == CONTROL_OK)
          addTerritory(&(fld[i][j]), fld[i][j].clr);
      }
      magnitude(&(fld[i][j]), &magn);
      setInitialFunds(&(fld[i][j]), magn);
    }
  }

}



void beginTurn(color clr){

  int i, j;
  field ***territories;
  int *territoriesNumber;
  int magn, cost;

  switch(clr){
    case(LIGHTGREEN):
      territories = &lightGreenTerritories;
      territoriesNumber = &lightGreenTerritoriesNumber;
      break;
    case(DARKGREEN):
      territories = &darkGreenTerritories;
      territoriesNumber = &darkGreenTerritoriesNumber;
      break;
    case(YELLOW):
      territories = &yellowTerritories;
      territoriesNumber = &yellowTerritoriesNumber;
      break;
    case(BROWN):
      territories = &brownTerritories;
      territoriesNumber = &brownTerritoriesNumber;
      break;
    case(BLUE):
      territories = &blueTerritories;
      territoriesNumber = &blueTerritoriesNumber;
      break;
    case(RED):
      territories = &redTerritories;
      territoriesNumber = &redTerritoriesNumber;
      break;
    case(PINK):
      territories = &pinkTerritories;
      territoriesNumber = &pinkTerritoriesNumber;
      break;
    case(BLACK):
      territories = &blackTerritories;
      territoriesNumber = &blackTerritoriesNumber;
      break;
  }

  for(i=0; i<(*territoriesNumber); i++){
    magn = 0;
    cost = 0;
    magnitude((*territories)[i], &magn);
    for(j=0; j<(*territories)[i]->numberHumans; j++){
      if((*territories)[i]->Humans[j]->lvl == MILITIA)
        cost += 2;
      else if((*territories)[i]->Humans[j]->lvl == SOLDIER)
        cost += 6;
      else if((*territories)[i]->Humans[j]->lvl == WARRIOR)
        cost += 18;
      else if((*territories)[i]->Humans[j]->lvl == CHAMPION)
        cost += 54;
    }

    (*territories)[i]->funds += magn;
    (*territories)[i]->funds -= cost;

    if((*territories)[i]->funds < 0){
      (*territories)[i]->funds = 0;
      freeHumans((*territories)[i]);
    }

  }

}

void beginDay(field **fld){

  int i, j;

  for(i=0; i<30; i++){
    for(j=0; j<30; j++){
      if(fld[i][j].isCapital == FALSE && fld[i][j].isFort == FALSE){
        if(fld[i][j].whoHere == NULL){
          if(rand()%20 == 0)
            fld[i][j].isTree = TRUE; 
        }
      }
    }
  }

}

void linkHexagons(field **fld, field *current, int i, int j, int side){

  if(i < 0 || j < 0 || i > 29 || j > 29){
    current->mapNeighs[side] = NULL;
    return;
  }
    
  if(fld[i][j].clr == 0x00){
    current->mapNeighs[side] = NULL;
  }
  else{
    current->mapNeighs[side] = &(fld[i][j]);
    fld[i][j].mapNeighs[(side+3)%6] = current;
  }

}

void magnitude(field *root, int *magn){

  int i;

  if(root == NULL)
    return;

  (*magn)++;

  for(i=0; i<6; i++){
    magnitude(root->territoryNeighs[i], magn);
  }

}

void setInitialFunds(field *capital, int magn){

  capital->funds = 5 * magn;

}
